/**
 * 
 */
package com.zufangbao.earth.web.controller.system;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.zufangbao.earth.handler.RentNotifyHandler;
import com.zufangbao.gluon.handler.ResultHandler;
import com.zufangbao.gluon.spec.global.GlobalMsgSpec;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.entity.customer.Customer;
import com.zufangbao.sun.entity.order.Order;
import com.zufangbao.sun.entity.sms.AppSmsConfig;
import com.zufangbao.sun.service.AppService;
import com.zufangbao.sun.service.AppSmsConfigService;
import com.zufangbao.sun.service.OrderService;

/**
 * @author wukai
 *
 */
@Controller
@MenuSetting("menu-system")
public class RentNotifyController {
	
	@Autowired
	private RentNotifyHandler rentNotifyHandler ;
	
	@Autowired
	private ResultHandler resultHandler;
	
	@Autowired
	private AppService appService;
	
	@Autowired
	private AppSmsConfigService appSmsConfigService;
	
	@Autowired
	private OrderService orderService;
	
	private static final Log logger = LogFactory.getLog(RentNotifyController.class);
	
	@RequestMapping("/rent-notify")
	@MenuSetting("submenu-notify-renter")
	public ModelAndView rentNotifyIndex(){
		
		return new ModelAndView("sms/rent-notify");
	}
	@RequestMapping("/do-rent-notify")
	public @ResponseBody String notifyRenter(@RequestParam("orderNo")String orderNo){
		
		try {
			if(StringUtils.isEmpty(orderNo)){
				
				return resultHandler.errorJsonResult(GlobalMsgSpec.MSG_FAILURE); 
			}
			
			Order order = orderService.getOrder(orderNo);
			
			Customer customer = order.getCustomer();
			
			App app = customer.getApp();
			
			AppSmsConfig appSmsConfig = appSmsConfigService.getSmsConfig4App(app);
			
			return !rentNotifyHandler.sendSms4NotifyPayRent(order, appSmsConfig) ? resultHandler.errorJsonResult(GlobalMsgSpec.MSG_FAILURE) : resultHandler.sucJsonResult();
			
		} catch (Exception e) {
	
			logger.error("rent notify occur error["+e.getMessage()+"]");
			
			return resultHandler.errorJsonResult(GlobalMsgSpec.MSG_FAILURE);
		}
	}

}
